package com.vajava.str.model;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

@Entity(name = "Build")
public class Build {
    private Long id;
    private Calendar timestamp;
    private String label;
    private List<Batch> batches;
    private BuildStatus status;

    private static final List<String> EQHA_IGNORE = Arrays.asList("id", "batches");

    public Build(Calendar timestamp, String label) {
        this.timestamp = timestamp;
        this.label = label;
        this.batches = new ArrayList<Batch>();
        this.status = BuildStatus.VALID;
    }

    protected Build() {
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Calendar getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Calendar timestamp) {
        this.timestamp = timestamp;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    @OneToMany(
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            mappedBy = "build")
    public List<Batch> getBatches() {
        return batches;
    }

    public void addBatch(Batch batch) {
        getBatches().add(batch);
    }

    public void setBatches(List<Batch> batches) {
        this.batches = batches;
    }

    @Enumerated(EnumType.STRING)
    public BuildStatus getStatus() {
        return status;
    }

    public void setStatus(BuildStatus status) {
        this.status = status;
    }

    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj, EQHA_IGNORE);
    }

    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this, EQHA_IGNORE);
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
